package org.android.brasil.projetos.oktoberfest2011.view;

import java.util.List;

import org.android.brasil.projetos.oktoberfest2011.R;
import org.android.brasil.projetos.oktoberfest2011.negocio.model.Tweet;
import org.android.brasil.projetos.oktoberfest2011.utils.ControleDeBackground;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TwitterItemAdapter extends ArrayAdapter<Tweet> {
	
	private List<Tweet> tweets;

	public TwitterItemAdapter(Context context, int textViewResourceId, List<Tweet> tweets) {
		super(context, textViewResourceId, tweets);
		this.tweets = tweets;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		LayoutInflater vi = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v = vi.inflate(R.layout.twitter_item_view, null);

		Tweet tweet = tweets.get(position);
		if (tweet != null) {
			TextView username = (TextView) v.findViewById(R.idTwitter_item.username);
			TextView message = (TextView) v.findViewById(R.idTwitter_item.message);
			TwitterImageView image = (TwitterImageView) v.findViewById(R.idTwitter_item.avatar);
			LinearLayout ll = (LinearLayout) v.findViewById(R.idTwitter_item.linearLayoutItem);
			ControleDeBackground.setBackgroundTwitter(new View[]{ll}, new TextView[]{username, message}, position);
			
			if (username != null) {
				username.setText(tweet.getFromUser());
			}

			if (message != null) {
				message.setText(tweet.getText());
			}

			if (image != null) {
				image.setUsername(tweet.getFromUser());
				image.setRemoteURI(tweet.getProfileImageUrl());
				image.loadImage();
			}
		}
		return v;
	}
}
